<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改诊疗单')"/>
    <th:block th:include="include :: select2-css"/>

</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-ncZhenliaoLogs-edit" th:object="${ncZhenliaoLogs}">
        <!--药品ID-->
        <input id="treeId" name="treeId " type="hidden" value="1"/>

        <input id="zlId" name="zlId" th:field="*{zlId}" type="hidden">

        <div class="form-group" hidden="hidden">
            <label class="col-sm-3 control-label">诊疗状态：</label>
            <div class="col-sm-8">
                <input id="zlStatus" name="zlStatus" value="3" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">鸡舍号：</label>
            <div class="col-sm-8">
                <select name="roomNum" class="form-control m-b" th:with="type=${@dict.getType('chicken_juanshe_num')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{roomNum}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"> 发病次数：</label>
            <div class="col-sm-8">
                <input id="illMuns" name="illMuns" th:field="*{illMuns}" class="form-control" type="number">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">症状：</label>
            <div class="col-sm-8">
                <input id="symptom" name="symptom" th:field="*{symptom}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">诊疗人员：</label>
            <div class="col-sm-8">
                <select id="zlUser" name="zlUser" class="form-control select2-multiple">
                    <option th:each="user:${users}" th:value="${user.userId}" th:text="${user.loginName}"
                            th:field="*{zlUser}"></option>
                </select>
            </div>
        </div>
        <div class="form-group" id="drugArr">


        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">诊疗结果：</label>
            <div class="col-sm-8">
                <input id="zlResult" name="zlResult" th:field="*{zlResult}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">诊疗时间：</label>
            <div class="col-sm-8">
                <input id="zlTime" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss" required name="zlTime"
                       th:value="*{#dates.format(new java.util.Date().getTime(), 'yyyy-MM-dd HH:mm:ss')}"  class="form-control" type="text"
                       th:class="time-input">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea rows="4" name="remark" th:field="*{remark}" class="form-control" type="text"></textarea>
            </div>
        </div>
        <div>
            <button class="btn btn-new btn-primary btn-circle btn-lg" style="margin: 0px auto;display: table;"
                    type="button"><i
                    class="fa fa-plus"></i>
            </button>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/ncZhenliaoLogs/chicken";



    /*移除一行*/
    function doRemove(e) {
        $(e).parents(".drug").remove();
    }
    var index = 0;
    //新增药品输入框
    $('.btn-new').click(function () {
        index++;
        addRows(index);
    });

    function addRows() {
        //药品数据
        var drugs = [[${drugs}]];
        //用药方法
        var eType = [[${eType}]];
        //用药单位
        var unit = [[${unit}]];
        var drug =
            '<div class="drug">' +
            '<div class="form-group">' +
            '            <label class="col-sm-3 control-label">药品名称：</label>' +
            '            <div class="col-sm-8">' +
            '                <select id="drugId" name="drugId_ ' + index + '" class="form-control select2-multiple">';


        for (var i = 0; i < drugs.length; i++) {
            drug += "<option value='" + drugs[i].drugId + "'>"  + drugs[i].drugCode + '-' +  drugs[i].drugName +  "</option>"
        }


        drug += '                </select>' +
            '            </div>' +
            '        </div>' +
            '        <div class="form-group">' +
            '            <label class="col-sm-3 control-label">剂量：</label>' +
            '            <div class="col-sm-8">' +
            '                <input id="drugAmount" name="drugAmount_ ' + index + '"  class="form-control" type="number">' +
            '            </div>' +
            '        </div>' +
            '        <div class="form-group">' +
            '            <label class="col-sm-3 control-label">药品方法：</label>' +
            '            <div class="col-sm-8">' +
            '                  <select id="drugMethod" name="drugMethod_' + index + '" class="form-control select2-multiple">';


        for (var i = 0; i < eType.length; i++) {
            drug += "<option value='" + eType[i].dictValue + "'>" + eType[i].dictLabel + "</option>"
        }


        drug += '                </select>' +
            '            </div>' +
            '        </div>' +
            '        <div class="form-group">' +
            '            <label class="col-sm-3 control-label">单位：</label>' +
            '            <div class="col-sm-8">' +
            '                <select id="unit" name="unit_' + index + '"   class="form-control select2-multiple">';


        for (var i = 0; i < unit.length; i++) {
            drug += "<option value='" + unit[i].dictValue + "'>" + unit[i].dictLabel + "</option>"
        }


        drug += '                </select>' +
            '            </div>' +
            '               <button onclick="doRemove(this)" class="btn btn-danger btn-circle" type="button"><i class="fa fa-remove"></i></button> ' +
            '        </div>' +
            '        </div>';

        $('#drugArr').append(drug);
        $('#drugArr').change();
    }



    function form2JsonString(formId) {
        var paramArray = $('#' + formId).serializeArray();
        /*请求参数转json对象*/
        var jsonObj = {};
        $(paramArray).each(function () {
            jsonObj[this.name] = this.value;
        });
        return jsonObj;
    }

    function submitH() {
        if ($.validate.form()) {
            var list = [];
            $('.drug').each(function (i, e) {
                var e1 = $(e).find('select')[0];
                var e2 = $(e).find('input')[0];
                var e3 = $(e).find('select')[1];
                var e4 = $(e).find('select')[2];
                var obj = {drugId: $(e1).val(), drugAmount: $(e2).val(), executeType: $(e3).val(), unit: $(e4).val()};
                list.push(obj)
            });
            return list;

        }
    }
    $("#form-ncZhenliaoLogs-edit").validate({
        rules: {
            illMuns: {
                required: true,
                digits: true
            }
        },
        messages: {
            illMuns: "发病次数必须为正整数"
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = form2JsonString("form-ncZhenliaoLogs-edit");
            data.yaos = submitH();
            myPost(prefix + "/edit", data,
                function (data) {
                    if (data.code == 0) {
                        $.modal.msgReload(data.msg,modal_status.SUCCESS)
                        parent.$('bootstrap-table-mianyi-logs-status2').bootstrapTable('refresh', {
                            silent: true
                        });
                        parent.$('bootstrap-table-mianyi-logs-status1').bootstrapTable('refresh', {
                            silent: true
                        });
                    } else {
                        $.modal.msg(data.msg,modal_status.FAIL)
                        parent.$('bootstrap-table-mianyi-logs-status2').bootstrapTable('refresh', {
                            silent: true
                        });
                        parent.$('bootstrap-table-mianyi-logs-status1').bootstrapTable('refresh', {
                            silent: true
                        });
                    }
                } )
        }
    }

</script>
</body>
</html>
